<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Tools\Func;
use App\Models\AdminUser;
use App\Models\KeShi;
use App\Models\Yisheng;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;


class YishengController extends Controller
{
    use Func;

    // 科室列表
    public function yishengList(Request $request)
    {
        $keywords = trim($request->keywords);
        $data = Yisheng::with('keshi')->where(function ($query) use($keywords) {
            // 关键字
            if ($keywords) {
                $query->orWhere('yisheng_name', $keywords);
            }
        })->paginate($request->limit ?? 10);
        return $this->returnJson(0, 'success', $data);
    }
	
	//修改状态
	public function editStatus(Request $request)
	{
		if (!Yisheng::where(['id' => $request->id])->update(['status' => $request->status])) {
			
            return $this->returnJson(1, '修改失败');
        }

        return $this->returnJson(0, '修改成功');
	}
	

    // 添加
    public function addYisheng(Request $request)
    {
        if (!$res = Yisheng::create([
            'yisheng_name' => $request->yisheng_name,
			'keshi_id' => $request->keshi_id,
			'imageUrl' => $request->imageUrl,
			'jianjie' => $request->jianjie,
            'created_at' => date('Y-m-d H:i:s')
        ])) {
            return $this->returnJson(1, '添加失败', $res);
        }

        return $this->returnJson(0, '添加成功', $res);
    }

    // 删除合同
    public function delYisheng(Request $request)
    {
        if (!Yisheng::where('id', $request->id)->delete()) {
            return $this->returnJson(1, '删除失败');
        }

        return $this->returnJson(0, '删除成功');
    }

    // 获得科室信息
    public function getYishengInfo(Request $request)
    {

        $res = Yisheng::where('id', $request->id)->first();

        return $this->returnJson(0, 'success', $res);
    }

    // 编辑合同信息
    public function editYisheng(Request $request)
    {
        // dd($request->addAccount);
        if (!$res = Yisheng::where('id', $request->id)->update([
            'yisheng_name' => $request->yisheng_name,
			'keshi_id' => $request->keshi_id,
			'imageUrl' => $request->imageUrl,
			'jianjie' => $request->jianjie,			
			
        ])) {
            return $this->returnJson(1, '修改失败', $res);
        }

        return $this->returnJson(0, '修改成功', $res);       
    }
	
	//头像上传
	public function uploadPic(Request $request)
	{
		// dd($request->file);
		
		// 上传文件
		$path = $request->file('pic')->store('img');
		
		// 上传文件的完整路径
		$filePath =  public_path().'/'.$path;
		
		if ($request->type) {
			return [
				    "errno" => 0, // 注意：值是数字，不能是字符串
				    "data" => [
				        "url" => asset('app/' . $path), // 视频 src ，必须
				        "poster" => "" // 视频封面图片 url ，可选
				    ]
			];
		}
		return $this->returnJson(0, 'success', asset('app/' . $path));
	}

}
